<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  label-position="right" label-width="100px" class="demo-ruleForm" size="large" >
    <el-form-item label="ItemID"  prop="itemId">
      <el-input v-model="ruleForm.itemId" style="width:300px"></el-input>
    </el-form-item>


    <el-form-item label="ProductId" prop="productId">
      <el-select v-model="ruleForm.productId" placeholder="请选择productId ">
        <el-option v-for="product in products"
                   :label="product.productId"
                   :value="product.productId"
        ></el-option>

      </el-select>
    </el-form-item>

    <el-form-item label="ListPrice" prop="listPrice">
      <el-input v-model="ruleForm.listPrice"style="width:300px"></el-input>
    </el-form-item>
    <el-form-item label="UnitCost" prop="unitCost">
      <el-input v-model="ruleForm.unitCost"style="width:300px"></el-input>
    </el-form-item>
    <el-form-item label="SupplierId" prop="supplierId">
      <el-input v-model="ruleForm.supplierId"style="width:300px"></el-input>
    </el-form-item>
    <el-form-item label="Status" prop="status">
      <el-input v-model="ruleForm.status"style="width:300px"></el-input>
    </el-form-item>
    <el-form-item label="Quantity" prop="quantity">
      <el-input v-model="ruleForm.quantity"style="width:300px"></el-input>
    </el-form-item>
    <el-form-item label="Attribute1" prop="attribute1">
    <el-input v-model="ruleForm.attribute1"style="width:300px"></el-input>
  </el-form-item>
    <el-form-item label="Attribute2" prop="attribute2">
      <el-input v-model="ruleForm.attribute2"style="width:300px"></el-input>
    </el-form-item>
    <el-form-item label="Attribute3" prop="attribute3">
      <el-input v-model="ruleForm.attribute3"style="width:300px"></el-input>
    </el-form-item>
    <el-form-item label="Attribute4" prop="attribute4">
      <el-input v-model="ruleForm.attribute4"style="width:300px"></el-input>
    </el-form-item>
    <el-form-item label="Attribute5" prop="attribute5">
      <el-input v-model="ruleForm.attribute5"style="width:300px"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        itemId:'',
        productId: '',
        listPrice:'',
        unitCost:'',
        supplierId:'',
        status:'',
        attribute1:'',
        attribute2:'',
        attribute3:'',
        attribute4:'',
        attribute5:'',
        quantity:''
      },
      rules: {
        itemId: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        productId: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        listPrice: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        supplierId: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        unitCost: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        attribute1: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        attribute2: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        attribute3: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        attribute4: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        attribute5: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        quantity: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ]
      },
      products:[{
        productId:'AV-CB-01',
      }]
    };
  },
  created() {
    const _this = this
    //此处需要查询item的全部信息
    axios({
      method:"GET",
      url:'http://115.29.176.82:8081/manager/api/product',
      params:{
        size:500,
        page:1
      }
    }).then(function (resp){
      _this.products = (resp.data.data.list)
    })

  },
  methods: {
    submitForm(formName) {
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios({
            method: "POST",
            url: 'http://115.29.176.82:8081/manager/api/item',
            data: {
              productId: _this.ruleForm.productId,
              itemId: _this.ruleForm.itemId,
              listPrice: parseFloat(_this.ruleForm.listPrice),
              unitCost: parseFloat(_this.ruleForm.unitCost),
              supplierId: parseInt(this.ruleForm.supplierId),
              status: _this.ruleForm.status,
              attribute1: _this.ruleForm.attribute1,
              attribute2: _this.ruleForm.attribute2,
              attribute3: _this.ruleForm.attribute3,
              attribute4: _this.ruleForm. attribute4,
              attribute5: _this.ruleForm.attribute5,
              quantity: parseInt(_this.ruleForm.quantity),
            }
          }).then(function (resp){
            console.log(resp)
            if(resp.status===200)
              alert('添加成功，请继续您的操作')
          })
        } else {
             alert('添加失败，请核实数据后，再次添加')
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>